package com.cb.domain.service.system;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cb.domain.model.order.Policy;
import com.cb.domain.service.excelWork.AbrExcelWorker;
import com.cb.domain.service.excelWork.POLICY_TYPE;
import com.cb.domain.service.excelWork.WORK_TYPE;
import com.cb.domain.service.excelWork.Impl.ApplyPolicyWorker;
import com.cb.domain.service.excelWork.Impl.CommonWorker;
import com.cb.domain.service.excelWork.Impl.CustomerPolicyWorker;
import com.cb.domain.service.excelWork.Impl.LowPriceWorker;
import com.cb.domain.service.excelWork.Impl.PrepayWorker;
import com.cb.domain.service.excelWork.formWork.BaseFormWorker;
import com.cb.domain.service.excelWork.formWork.Impl.FormApplyPolicyWorker;
import com.cb.domain.service.excelWork.formWork.Impl.FormCommonWorker;
import com.cb.domain.service.excelWork.formWork.Impl.FormCustomerPolicyWorker;
import com.cb.domain.service.excelWork.formWork.Impl.FormLowPriceWorker;
import com.cb.domain.service.excelWork.formWork.Impl.FormPrepayWorker;




@Service
public class ExlDataManager{
	private final static Logger logger=Logger.getLogger(ExlDataManager.class);
	@Autowired
	private ApplyPolicyWorker applyWorker;
	@Autowired
	private CommonWorker commonWorker;
	@Autowired
	private PrepayWorker prepayWorker;
	@Autowired
	private CustomerPolicyWorker customerWorker;
	@Autowired
	private LowPriceWorker lowPriceWorker;
	@Autowired
	private FormApplyPolicyWorker formApplyWorker;
	@Autowired
	private FormCustomerPolicyWorker formCustomerWorker;
	@Autowired
	private FormPrepayWorker formPrepayWorker;
	@Autowired
	private FormLowPriceWorker formLowpriceWorker;
	@Autowired 
	private FormCommonWorker formCommonWoker;
	
	private List<Policy> policyList;
	
	public List<Policy> getPolicyList() {
		return policyList;
	}

	public void setPolicyList(List<Policy> policyList) {
		this.policyList = policyList;
	}

	/**
	 * 根据EXCEL发送XML内容到接口地址
	 * @param type 政策类型
	 * @return 接口返回信息
	 */
	public String doWorkByType(POLICY_TYPE type,String path)throws Exception{
		String backInfo="";
		AbrExcelWorker worker=createWorkerByType(type);
		if(null!=worker&&StringUtils.isNotEmpty(path)){

				worker.init(path, type);
				String xmlContent=worker.doWork();
				policyList=worker.getPolicyList();
//				backInfo=postXmlContent(xmlContent);
	
		}
		return backInfo;
	}
	
	/**
	 * 根据页面BEAN生成XML内容发送接口地址
	 * @param type 政策类型
	 * @param policy 页面BEAN
	 * @return 接口返回信息
	 */
	public String doFormWorkByType(POLICY_TYPE type,Policy policy){
		String backInfo="";
		BaseFormWorker worker=createFormWorkerByType(type);
		if(null!=worker){
			String xmlContent=worker.doWork(policy, WORK_TYPE.ADD, type);
			backInfo=postXmlContent(xmlContent);
		}
		return backInfo;
	}
	
	
	public String postXmlContent(String content){
	 	String backinfo = "";
		long l=System.currentTimeMillis();
		HttpClient client = new HttpClient();
		client.getHostConfiguration().setProxy("10.123.74.137", 808);
		PostMethod method = new PostMethod("http://tts.mtrip.net/tts/interface/policy.jsp");
		try {
			method.setRequestEntity(new ByteArrayRequestEntity(gZip(content), "multipart/form-data"));
			client.executeMethod(method);
			backinfo=method.getResponseBodyAsString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		method.releaseConnection();
		System.out.println(System.currentTimeMillis()-l);
        return backinfo;
	}
	public static byte[] gZip(String data) throws IOException {
        if (null == data || 0 == data.length()) {
            return new byte[0];
        }
        byte[] b = null;
        ByteArrayOutputStream bos = null;
        GZIPOutputStream gzip = null;
        try {
            bos = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(bos);
            gzip.write(data.getBytes("UTF-8"));
            gzip.finish();
            gzip.close();
            b = bos.toByteArray();
        } finally {
            if (null != bos) {
                bos.close();
            }
        }
        return b;
    }
	
	private AbrExcelWorker createWorkerByType(POLICY_TYPE type){
		AbrExcelWorker worker=null;
		switch (type) {
		case APPLY: worker=applyWorker;
			break;
		case COMMON:worker=commonWorker;
			break;
		case PREPAY:worker=prepayWorker;
			break;
		case LOWPRICE:worker=lowPriceWorker;
			break;
		case CUSTOMER:worker=customerWorker;
			break;
		default:
			break;
		}
		return worker;
	}
	
	private BaseFormWorker createFormWorkerByType(POLICY_TYPE type){
		BaseFormWorker worker=null;
		switch (type) {
		case APPLY: worker=formApplyWorker;
			break;
		case COMMON:worker=formCommonWoker;
			break;
		case PREPAY:worker=formPrepayWorker;
			break;
		case LOWPRICE:worker=formLowpriceWorker;
			break;
		case CUSTOMER:worker=formCustomerWorker;
			break;
		default:
			break;
		}
		return worker;
	}
	
	public static void main(String[] args) throws Exception {
		long l=System.currentTimeMillis();
		HttpClient client = new HttpClient();
		 client.getHostConfiguration().setProxy("10.123.74.137",808);

		PostMethod method = new PostMethod("http://tts.mtrip.net/tts/interface/policy.jsp");
		String content="<Content name='abc'></Content>";
		method.setRequestEntity(new ByteArrayRequestEntity(gZip(content), "multipart/form-data"));
		client.executeMethod(method);
//		client.notify();
		System.out.println(method.getResponseBodyAsString());
		method.releaseConnection();
		System.out.println(System.currentTimeMillis()-l);
	}
}
